/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.viettel.bkno.visionone.services;

import com.viettel.bkno.visionone.entities.DisplayData;
import com.viettel.bkno.visionone.entities.Resource;
import com.viettel.bkno.visionone.exception.CoreServiceFault;
import com.viettel.bkno.visionone.exception.CoreServiceException;
import com.viettel.bkno.visionone.untils.KeyGenerator;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import org.apache.log4j.Logger;

/**
 *
 * @author hien
 */
@WebService(serviceName = "agent")
public class AgentServices {
    static Logger logger = Logger.getLogger(AgentServices.class);

    static {
        ServicesManager.getInstance().start();
    }
            
    /**
     * This is a sample web service operation
     * @param txt
     * @return 
     */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        return "Hello " + txt + " !";
    }

    /**
     * Web service operation
     * @param macAddress
     * @return isAttached
     */
    @WebMethod(operationName = "isAttached")
    public Boolean isAttached(@WebParam(name = "macAddress") String macAddress) {
        //TODO checkExistMac
        // Kiem tra trong database xem dia chi mac nay da ton tai chua
        // Neu ton tai thi tra ve true
        // Neu khong ton tai tra ve false
        
        return true;
    }

    /**
     * Web service operation
     * @param macAddress
     * @param scrWidth
     * @param scrHeight
     * @return 
     */
    @WebMethod(operationName = "requestAttach")
    public String requestAttach(@WebParam(name = "macAddress") String macAddress, @WebParam(name = "scrWidth") int scrWidth, @WebParam(name = "scrHeight") int scrHeight) {
        //TODO request Attach
        // Kiem tra dia chi mac da co chua? 
        // - neu co roi thi tra ve false
        // - neu device chua co trong he thong:
        String pairKey = KeyGenerator.getInstance().genPairkey();
        // + Luu device vao he thong
        // + Thiet lap trang thai chua attach (status = WAIT)
        // + Thiet lap key pair = pairKey
        // + Neu khong co loi thi tra ve true
        // + Neu co loi thi xoa device trong he thong tra ve false
        
        return pairKey;
    }

    /**
     * Web service operation
     * @return 
     */
    @WebMethod(operationName = "getServerTime")
    public String getServerTime() {
        //TODO write your implementation code here:
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        String currentTime = format.format(date);
        return currentTime;
    }

    /**
     * Web service operation
     * @param mac
     * @return 
     */
    @WebMethod(operationName = "getNewResources")
    public ArrayList<Resource> getNewResources(@WebParam(name = "mac") String mac) {
        ArrayList<Resource> newResources = new ArrayList<>();
        //TODO: Lay ra cac resource moi add vao device 
        // Lay ra new resource cua device co dia chi mac = mac        
        
        return newResources;
    }

    /**
     * Web service operation
     * @param mac
     * @return 
     */
    @WebMethod(operationName = "getDeleteResources")
    public ArrayList<Resource> getDeleteResources(@WebParam(name = "mac") String mac) {
        ArrayList<Resource> deletedResources = new ArrayList<>();
        //TODO lay ra cac resource moi bi remove khoi device
        
        return deletedResources;
    }

    /**
     * Web service operation
     * @param mac
     * @return 
     */
    @WebMethod(operationName = "getNewSchedules")
    public ArrayList<DisplayData> getNewAndUpdateSchedules(@WebParam(name = "mac") String mac) {
        ArrayList<DisplayData> schedules = new ArrayList<>();
        //TODO: getNewAndUpdateSchedules
        
        return schedules;
    }

    /**
     * Web service operation
     * @param name
     * @return 
     * @throws com.viettel.bkno.visionone.exception.CoreServiceException 
     */
    @WebMethod(operationName = "testException")
    public String testException(@WebParam(name = "name") String name) throws CoreServiceException {
        //TODO write your implementation code here:
        if (name.equals("hien")) {
            CoreServiceFault fault = new CoreServiceFault();
            fault.setFaultCode("ERR001");
            fault.setFaultString("Your name can not be 'hien'");
            throw  new CoreServiceException(fault);
        }
        return name;
    }
}
